/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.mbfasttrack.controller;

import com.mbfasttrack.model.Geonames;
import com.mbfasttrack.service.GeonamesService;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 */
@Controller
public class GeonamesController extends Base {

    /**
     * 
     */
    @Value("${sys.geonames.url}")
    public String sys_geonames_url;

    /**
     * 
     * @return
     */
    @ModelAttribute("geonames")
    public Geonames getGeonamesObject() {
        return new Geonames();
    }

    /**
     * 
     * @param geonamesService 
     */
    @Autowired(required = true)
    public void setGeonamesService(GeonamesService geonamesService) {
        this.geonamesService = geonamesService;

    }

    /**
     *  Json con il risultato per le autocomplete
     * 
     * @param like
     * @param response
     * @throws JsonGenerationException
     * @throws JsonMappingException
     * @throws IOException
     */
    @RequestMapping(value = "/geonames/json/search", method = RequestMethod.GET)
    public void jsonSearch(@RequestParam("term") String like, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {

        List<Geonames> li = this.geonamesService.getAllByLikeLoc(like);

        ObjectMapper mapper = new ObjectMapper();

        HashMap result = new HashMap();
        result.put("geonames", li);

        String jsonString = mapper.writeValueAsString(result);

        AbstractHttpMessageConverter<String> stringHttpMessageConverter = new StringHttpMessageConverter();

        MediaType jsonMimeType = MediaType.APPLICATION_JSON;

        if (stringHttpMessageConverter.canWrite(String.class, jsonMimeType)) {

            try {

                stringHttpMessageConverter.write(jsonString, jsonMimeType, new ServletServerHttpResponse(response));

            } catch (Exception e) {
                log.fatal(e);
                System.err.print("exception " + e.getMessage());
            }

        }

    }

    /**
     * 
     * @return
     */
    @RequestMapping(value = "/data/importgeonames")
    public String importGeonamesData() {
        geonamesService.importData(sys_geonames_url);
        return "index";

    }
}
